/* =============================================================
 * Your Grandmother's guide to Algorithms
 * =============================================================
 *
 * Copyright (c) 2007, Saverio Miroddi
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright notice,
 *       this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright 
 *       notice, this list of conditions and the following disclaimer in the 
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the SaverioMiroddiSoftware nor the names of its 
 *       contributors may be used to endorse or promote products derived from 
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *  
 * ---------------------------
 * GuiBTreeNode.java
 * ---------------------------
 *
 */
package treegui.nodewrappers;

import java.awt.Color;
import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import btree.AbstractNode;

public class GuiBTreeNode implements IGuiNode {
	public static final Color INTERNAL_COLOR = Color.GREEN;
	public static final Color LEAF_COLOR = Color.RED;
	
	private final AbstractNode _node;
	private Point _position;
	
	public GuiBTreeNode(AbstractNode node) {
		_node = node;
		_position = null;
	}

	@Override
	public List<IGuiNode> getChildren() {
		List<IGuiNode> result = new ArrayList<IGuiNode>();
		
		for (AbstractNode node : _node.getChildren()) {
			if (node == null) break;
			else result.add(new GuiBTreeNode(node));
		}
		
		return result;
	}
	
	@Override
	public List<EdgeCoordinate> getEdgeCoordinates() {
		List<EdgeCoordinate> edges = new ArrayList<EdgeCoordinate>();
		
		List<AbstractNode> currLevelNodes = new ArrayList<AbstractNode>();
		currLevelNodes.add(_node);
		
		while (currLevelNodes.size() > 0) {
			List<AbstractNode> nextLevelNodes = new ArrayList<AbstractNode>(); 
			for (AbstractNode parent : currLevelNodes) {
				IGuiNode guiParentNode = new GuiBTreeNode(parent); 

				// first, children
				for (AbstractNode child : parent.getChildren()) {
					IGuiNode guiChildNode = new GuiBTreeNode(child); 
					edges.add(new EdgeCoordinate(guiParentNode, guiChildNode));
					nextLevelNodes.add(child);
				}
				
				// last, right ptr
				if (parent.getRightPtr() != null) {
					AbstractNode rightNode = parent.getRightPtr();
					IGuiNode guiRightNode = new GuiBTreeNode(rightNode); 
					edges.add(new EdgeCoordinate(guiParentNode, guiRightNode));
					nextLevelNodes.add(parent.getRightPtr());
				}
			}
			currLevelNodes = nextLevelNodes;
		}
		
		return edges; 		
	}

	@Override
	public Color getColor() {
		if (_node.getChildren().size() == 0) return LEAF_COLOR; 
		else return INTERNAL_COLOR;
	}

	@Override
	public Point getPosition() {
		return _position;
	}

	@Override
	public String toString() {
		return _node.toString();
	}

	@Override
	public void setPosition(Point position) {
		_position = position;
	}

	@Override
	public boolean equals(Object other) {
		return (other instanceof GuiBTreeNode) &&
			   (_node.equals(((GuiBTreeNode)other)._node));
	}
}